package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.bean.JqdbMaterialListCheck;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqdbMaterialListCheckMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqdbMaterialListMapper;
import com.jy.adscm.operation.mscyb.service.JqdbMaterialListCheckService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.service.JqdbMaterialListService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-18
 */
@Service
public class JqdbMaterialListCheckServiceImpl extends ServiceImpl<JqdbMaterialListCheckMapper, JqdbMaterialListCheck> implements JqdbMaterialListCheckService {
    @Autowired
    JqdbMaterialListMapper jqdbMaterialListMapper;
    @Autowired
    JqdbMaterialListService jqdbMaterialListService;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public boolean updatejqdbMaterialListCheck(List<JqdbMaterialListCheck> jqdbMaterialListChecks, Token token) throws AdscmSystemException {

        for (JqdbMaterialListCheck materialListCheck : jqdbMaterialListChecks) {
            if ("结果".equals(materialListCheck.getName())){
                //查询到检查的物料
                JqdbMaterialList jqdbMaterialList = jqdbMaterialListMapper.selectById(materialListCheck.getPid());
                if ("1".equals(materialListCheck.getValue())){
                    jqdbMaterialList.setState("1");
                }
                if ("0".equals(materialListCheck.getValue())){
                    jqdbMaterialList.setState("0");
                }
                jqdbMaterialList.setCreator(token.getUser().getId().toString());
                jqdbMaterialList.setCreateTime(new Date());
                //修改这个物料
                jqdbMaterialListService.updateJqdbMaterialList(jqdbMaterialList);
            }
            boolean b = super.updateById(materialListCheck);
        }




        return true;
    }

    @Override
    public List<JqdbMaterialListCheck> selectByPid(String pid) {
        Wrapper<JqdbMaterialListCheck> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.orderBy("rank",false);
        List<JqdbMaterialListCheck> jqdbMaterialListChecks = super.selectList(wrapper);
        for (JqdbMaterialListCheck jqdbMaterialListCheck : jqdbMaterialListChecks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqdbMaterialListCheck.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqdbMaterialListCheck.getCreator());
                jqdbMaterialListCheck.setCreatorName(jyUser.getName());
            }
        }
        return jqdbMaterialListChecks;
    }
}
